package com.snail.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.snail.model.pojo.cart.BizOrder;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
@Mapper
public interface BizOrderMapper extends BaseMapper<BizOrder> {

    //根据用户id查询订单
    @Select("select * from biz_order where user_id = #{userId}")
    List<BizOrder> selectByUserId(Long userId);

    //生成/新增订单
    @Insert("insert into biz_order (order_no, user_id, total_amount, pay_amount, order_status, create_time, update_time) " +
            "values (#{orderNo}, #{userId}, #{totalAmount}, #{payAmount}, #{orderStatus}, #{createTime}, #{updateTime})")
    void createOrder(BizOrder bizOrder);

    //根据订单id查询订单
    @Select("select * from biz_order where order_id = #{orderId}")
    BizOrder selectByOrderId(Long orderId);
    //根据订单编号查询订单
    @Select("select * from biz_order where order_no = #{orderNo}")
    BizOrder selectByOrderNo(String orderNo);
    // 继承BaseMapper后，会自动获得selectPage等方法，无需手动定义
    // 以下是MyBatis-Plus内置的selectPage方法的等效声明（无需手动编写，仅作说明）
    /**
     * 分页查询方法
     * @param page 分页参数对象
     * @param queryWrapper 查询条件
     * @return 分页结果
     */
//    IPage<BizOrder> selectPage(IPage<BizOrder> page, @Param(Constants.WRAPPER) Wrapper<BizOrder> queryWrapper);

    @Select("SELECT * FROM biz_order WHERE order_id = #{orderId} FOR UPDATE")
    BizOrder selectByOrderIdForUpdate(Long orderId);
}
